package com.ruoyi.system.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.ImgUntil;
import com.ruoyi.common.utils.ImgUntilYS;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author zyy
 * @date 2024-07-06
 */
@Slf4j
public class MiMaUtil {

    //图片上传
    public static String uploadImg(String img){
        String string = DigestUtils.md5Hex(img);
        File file = null;
        try {
            URL url = new URL(img);
            BufferedImage image = ImageIO.read(url.openStream());
            byte[] bytes = ImgUntil.bufferedImageToByteArray(image);
            long srcSize = bytes.length;
            System.out.println(srcSize / 1024);
            if (srcSize / 1024 < 1800) {
                bytes = ImgUntilYS.compressPicForScale(bytes, 512l);
            }
            if (srcSize / 1024 > 4000) {
                bytes = ImgUntilYS.compressPicForScale(bytes, 2048l);
            }
            file = convert(bytes,string);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(file.getName());
        String time = String.valueOf(System.currentTimeMillis());
        String key = "goods/picture/"+time+"_"+DigestUtils.md5Hex(time)+"_"+ RandomUtil.randomNumbers(4)+"_"+RandomUtil.randomNumbers(4)+"_"+RandomUtil.randomNumbers(6)+".jpeg";
        String body1 = HttpRequest.get("https://api.mimaapp.cn/v1/common/qiniu/get_token").execute().body();
        String data = JSONObject.parseObject(body1).getString("data");
        String token = JSONObject.parseObject(data).getString("token");
        String body = HttpRequest.post("https://upload-z2.qiniup.com/").form("file", file).form("token", token)
                .form("key", key).form("fname", file.getName()).form("x:fileMD5", string).execute().body();
        return body;
    }

    public static File convert(byte[] byteArray,String s) throws IOException {
        File file = File.createTempFile(s.substring(0,13), ".jpg");
//        imgUrl = file.getPath();
        Path path = Paths.get(file.getAbsolutePath());
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(byteArray);
        }catch (Exception e){

        }
        return file;
    }


    public static String login(String username,String password){
        String e = password;
//        String t = "DzLALeVwKbgtcOitGPpiVLm94J0pdghb";
//        String a = "DzLALeVwKbgtcOit";
        String t = "CzgKbhidhLfVwtGeMAftcApZVLF44J0Z";
        String a = "CzgKbhidhLfVwtGe";
        try {
            String str = Ke(e, t, a);
            //https://api.mimaapp.cn/v4/user/password_login
            String url = "https://api.mimaapp.cn/v4/user/password_login";
            Map<String,Object> params = new TreeMap<>();
            params.put("phone",username);
            params.put("password",str);
            params.put("device_install_time","");
            params.put("device_register_time","");
            String body = HttpRequest.post(url).body(JSONObject.toJSONString(params)).header("Device","1").execute().body();
            return body;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        String e = "ff123123";
        String t = "DzLALeVwKbgtcOitGPpiVLm94J0pdghb";
        String a = "DzLALeVwKbgtcOit";

        String str = Ke(e, t, a);
        System.out.println("Line:1011 &#127841; str: " + str);
    }

    public static String Ke(String e, String t, String a) throws Exception {
        byte[] keyBytes = t.getBytes("UTF-8");
        byte[] ivBytes = a.getBytes("UTF-8");
        byte[] dataBytes = e.getBytes("UTF-8");

        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

        byte[] encryptedBytes = cipher.doFinal(dataBytes);
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }
}
